package com.ma.service.impl;

import com.ma.dao.GroupDao;
import com.ma.entity.GroupEntity;
import com.ma.service.GroupService;
import com.ma.vo.GroupVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GroupServiceImpl implements GroupService {
    private GroupDao groupDao;

    @Override
    public List<GroupVo> getGroup() {
        //获取所有分组信息
        List<GroupEntity> groupEntityList = groupDao.selectAllGroupEntity();

        //获取一级分组
        List<GroupEntity> groupOneList = groupEntityList.stream().filter(groupEntity -> {
            return groupEntity.getParentCode() == null;
        }).collect(Collectors.toList());

        //根据每个一级分组的信息获取二级分组
        List<GroupVo> vos = groupOneList.stream().map(groupOne -> {
            GroupVo groupVo = new GroupVo();
            groupVo.setId(groupOne.getId());
            groupVo.setGroupName(groupOne.getGroupName());
            List<GroupVo.GroupVo2> groupTwoList = groupEntityList.stream().map(groupEntity -> {
                //当前一级分组下的二级分组
                GroupVo.GroupVo2 groupVo2 = new GroupVo.GroupVo2();
                if (groupEntity.getParentCode() == groupOne.getGroupCode()) {
                    groupVo2.setId(groupEntity.getId());
                    groupVo2.setGroupName(groupEntity.getGroupName());
                    List<GroupVo.GroupVo3> groupVo3s = groupEntityList.stream().map(group -> {
                        //当前二级分组下的三级分组
                        GroupVo.GroupVo3 groupVo3 = new GroupVo.GroupVo3();
                        if (group.getParentCode() == groupEntity.getGroupCode()) {
                            groupVo3.setId(group.getId());
                            groupVo3.setGroupName(group.getGroupName());
                            return groupVo3;
                        }
                        return groupVo3;
                    }).collect(Collectors.toList());
                    groupVo2.setGroupVo3List(groupVo3s);
                }
                return groupVo2;
            }).collect(Collectors.toList());
            groupVo.setGroupVo2List(groupTwoList);
            return groupVo;
        }).collect(Collectors.toList());

        return vos;
    }
}
